//
//  UserManager.m
//  Safflower
//
//  Created by 袁亮 on 2016/11/28.
//  Copyright © 2016年 Migic_Z. All rights reserved.
//

#import "UserManager.h"

#define UserInfoKey @"UserInfoKey"
#define UserTokenKey @"UserTokenKey"

@implementation UserManager

+(UserManager *)shareUserManager
{
    static UserManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[UserManager alloc]init];
    });
    return manager;
}

+(void)saveUser:(UserModel *)user
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *userJson = [user yy_modelToJSONString];
    [defaults setObject:userJson forKey:UserInfoKey];
    
}

+(UserModel *)fetchUserInfo
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *userJson = (NSString *)[defaults objectForKey:UserInfoKey];
    UserModel *user = [UserModel yy_modelWithJSON:userJson];
    return user;
}

+(void)removeUser
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults removeObjectForKey:UserInfoKey];
    
}

+(BOOL)isLogin
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    id user = [defaults objectForKey:UserInfoKey];
    
    if (user != nil) {
        return YES;
    }
    return NO;
}

@end
